<?php

class Ticket extends CActiveRecord {
    /**
     * The followings are the available columns in table 'z_ticket':
     * @var integer $id_ticket
     * @var string $name
     * @var string $email
     * @var string $text
     * @var string $title
     * @var string $c_date
     * @var integer $readed
     */
    public $verifyCode;
    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'z_ticket';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array('name, email, text, title', 'required'),
                array('readed', 'numerical', 'integerOnly'=>true),
                array('c_date', 'safe'),
                array('verifyCode', 'captcha', 'allowEmpty' => !extension_loaded('gd'), 'captchaAction' => 'site/captcha'
                        ,'on'=>'captcha'),
                array('email', 'email'),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('id_ticket, name, email, text, title, c_date, readed', 'safe', 'on'=>'search'),
        );
    }
    public function safeAttributes() {

        return array(
                'captcha' => 'name, email, text, title, verifyCode',
                'nocaptcha' => 'name, email, text, title',
        );

    }
    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'id_ticket' => 'id',
                'name' => 'Название',
                'email' => 'Email',
                'text' => 'Текст',
                'title' => 'Заголовок',
                'c_date' => 'Дата создания',
                'readed' => 'прочитано?',
        );
    }
    public function getCountNew() {

        return Ticket::model()->count(array(
                'select'=>'title',
                'condition'=>'readed=:postID',
                'params'=>array(':postID'=>0),
        ));
    }

    public function itRead($id) {
        $check = Ticket::model()->findByPk($id)->readed;
        if($check) return 'id="read"';
        else return 'id="noread"';
    }
    public function reading($id) {
        $record = Ticket::model()->findByPk($id);
        $record->readed = 1;
        $record->save();
    }
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id_ticket',$this->id_ticket);

        $criteria->compare('name',$this->name,true);

        $criteria->compare('email',$this->email,true);

        $criteria->compare('text',$this->text,true);

        $criteria->compare('title',$this->title,true);

        $criteria->compare('c_date',$this->c_date,true);

        $criteria->compare('readed',$this->readed);

        return new CActiveDataProvider('Ticket', array(
                        'criteria'=>$criteria,
        ));
    }
}